from werkzeug.security import generate_password_hash, check_password_hash
from flask_jwt_extended import create_access_token, create_refresh_token
from core.models import db, BaseModel


class User(BaseModel):
    __tablename__ = 'auth_users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.Integer, nullable=False, unique=True)
    password = db.Column(db.String(255), nullable=False)

    def set_password(self, password):
        self.password = generate_password_hash(password)

    def verify(self, password):
        return check_password_hash(self.password, password)

    @property
    def token(self):
        access_token = create_access_token(identity=self.id, fresh=True)
        refresh_token = create_refresh_token(self.id)
        return {
            'access_token': access_token,
            'refresh_token': refresh_token
        }
